package com.example.leetcode.trainingcamp.week7.Thursday;

public class Offer69_I {

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == p || root == q) return root;
        if (root != null){
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            TreeNode right = lowestCommonAncestor(root.right,p,q);
            if (left != null && right != null) return root;
            if (left != null) return left;
            return right;
        }
        return null;
    }
}
